﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dzs.Web.Admin.inventory
{
    public partial class takestore : Web.Admin.BasePage
    {
        dzs.BLL.inventory bll = new dzs.BLL.inventory();
        dzs.BLL.takestorelog blltake = new dzs.BLL.takestorelog();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                Page.DataBind();
            }

        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        public DataSet BindData()
        {
            DataSet ds = GetDataPage();
            gridView.DataSource = ds;
            gridView.DataBind();
            return ds;

        }

        private DataSet GetData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtname.Text != "")
            {
                strWhere.AppendFormat(" and material like '%{0}%' ", txtname.Text);

            }
            if (txtproject.Text != "")
            {
                strWhere.AppendFormat(" and projectname like '%{0}%' ", txtproject.Text);

            }
            if (Session["login"] != null)
            {
                DataSet u = (DataSet)Session["login"];

                if (u.Tables[0].Rows[0]["roles_id"].ToString() == "2")
                {
                    strWhere.AppendFormat(" and uid={0}", u.Tables[0].Rows[0]["user_id"]);
                }
            }
            ds = bll.Gettakestore(strWhere.ToString());
            return ds;
        }
        private DataSet GetDataPage()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtname.Text != "")
            {
                strWhere.AppendFormat(" and material like '%{0}%' ", txtname.Text);

            }
            if (txtproject.Text != "")
            {
                strWhere.AppendFormat(" and projectname like '%{0}%' ", txtproject.Text);

            }
            if (Session["login"] != null)
            {
                DataSet u = (DataSet)Session["login"];

                if (u.Tables[0].Rows[0]["roles_id"].ToString() == "2")
                {
                    strWhere.AppendFormat(" and uid={0}", u.Tables[0].Rows[0]["user_id"]);
                }
            }
            ds = bll.GetinventoryListByPage(strWhere.ToString(), "", (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + 1, this.AspNetPager1.CurrentPageIndex * this.AspNetPager1.PageSize);

            this.AspNetPager1.RecordCount = bll.GetinventoryRecordCount(strWhere.ToString());
            
            return ds;
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
        }

        protected void btnout_Click(object sender, EventArgs e)
        {

            //DataSet ds = BindData();
            //string f = DateTime.Now.Ticks.ToString();
            //CreateExcel(ds, f + ".xls");
            DataSet ds = GetData();
            gridView.DataSource = ds;

            gridView.AllowPaging = false;
            gridView.Columns[0].Visible = false;
            gridView.DataBind();
            string f = DateTime.Now.Ticks.ToString();
            ExportExcel(gridView, f);
            gridView.DataSource = ds;
            gridView.AllowPaging = true;
            gridView.Columns[0].Visible = true;
            gridView.DataBind();

        }
        public void CreateExcel(DataSet ds, string FileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            string colHeaders = "", ls_item = "";
            int i = 0;

            //定义表对象与行对像，同时用DataSet对其值进行初始化 
            DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select("");
            colHeaders += "库存号" + "\t";
            colHeaders += "物料名称" + "\t";
            colHeaders += "仓位" + "\t";
            colHeaders += "仓位类型" + "\t";
            colHeaders += "仓库" + "\t";
            colHeaders += "仓位状态" + "\t";
            colHeaders += "数量" + "\t";
            colHeaders += "项目编号" + "\t";
            colHeaders += "需用单位" + "\t";
            colHeaders += "项目名称" + "\n";
            //向HTTP输出流中写入取得的数据信息 
            resp.Write(colHeaders);
            //逐行处理数据   
            foreach (DataRow row in myRow)
            {
                //在当前行中，逐列获得数据，数据之间以\t分割，结束时加回车符\n 
                for (i = 0; i < dt.Columns.Count - 1; i++)
                    ls_item += row[i].ToString() + "\t";
                ls_item += row[i].ToString() + "\n";
                //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据     
                resp.Write(ls_item);
                ls_item = "";
            }
            //写缓冲区中的数据到HTTP头文件中 
            resp.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //这里没有内容。
        }

        public void ExportExcel(System.Web.UI.WebControls.WebControl objControl, string strFileName)
        {

            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }
       

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "myset")
            {
                if (Session["login"] != null)
                {
                    DataSet u = (DataSet)Session["login"];
                    int uid = int.Parse(u.Tables[0].Rows[0]["user_id"].ToString());

                    Button button = (Button)e.CommandSource;
                    GridViewRow row = (GridViewRow)button.Parent.Parent;
                    TextBox tn = row.FindControl("txtnum") as TextBox;
                    if (tn != null)
                    {
                        string oldnum = row.Cells[5].Text.ToString();//获得第一个单元格的值
                        decimal newnum =decimal.Parse(tn.Text);
                        int inid = int.Parse(this.gridView.DataKeys[row.RowIndex].Values[0].ToString());//获得DataKeys的值
                        bll.Updatenums(inid, newnum);
                        dzs.Model.takestorelog t = new Model.takestorelog();
                        t.inid = inid;
                        t.oldnum = decimal.Parse(oldnum);
                        t.newnum = newnum;
                        t.uid = uid;
                        blltake.Add(t);
                        BindData();
                    }
                }
            }

        }

        protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}